home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2556 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.8 KB  |  59 lines

  1. Path: doc.ic.ac.uk!brj
  2. From: brj@doc.ic.ac.uk (Benjamin Jefferys)
  3. Newsgroups: comp.lang.c++
  4. Subject: Problem with generic classes/templates
  5. Date: 18 Jan 1996 12:35:55 -0000
  6. Organization: Dept. of Computing, Imperial College, University of London, UK.
  7. Distribution: world
  8. Message-ID: <4dlerb$r3r@finch.doc.ic.ac.uk>
  9. Reply-To: brj@doc.ic.ac.uk (Benjamin Jefferys)
  10. NNTP-Posting-Host: finch.doc.ic.ac.uk
  11. X-Newsreader: mxrn 6.18-23
  12.  
  13.  
  14.  
  15. Right. I've declared a generic class which contains a definition of
  16. another class which has a constructor function which requires arguments.
  17. My terminology is probably a bit dodgy, so here is an outline:
  18.  
  19. class meringue{
  20.   int ann;
  21.   float veronica;
  22.   public:
  23.   meringue(int fred, float john) { ann=fred; veronica=john; }
  24. };
  25.  
  26. template <class X> class marzipan<X>{
  27.   X ian;
  28.   public:
  29.   marzipan<X>(/* what goes here? */) { /* and what goes here? */ }
  30. }
  31.  
  32. Once I've got these, I try a:
  33.  
  34. marzipan<meringue> bob;
  35.  
  36. But how do I pass parameters to the constructor function of meringue?
  37. The way you do it with inherited functions is to pass them to the
  38. constructor function of the derived class, then give them to the bases
  39. from there. But how do I do this here - I have to cater for all possible
  40. Xs, which could take any number/type of parameters (or none at all).
  41. How do I do this? I understand that I can use "..." somehow, but my
  42. C++ book seems to skip over this, only saying that it exists if you want to
  43. pass variable numbers/types of parameters to a function, and that you need
  44. at least one properly defined parameter (eg. int function(int roger, ...); )
  45.  
  46. But how do you access the parameters that are passed? Is there another
  47. more elegant solution to my problem?
  48.  
  49. (As a matter of interest, the generic class(es) implements a doubly-linked
  50. list, which obviously has to store any class to be useful)
  51.  
  52. Thanks in advance for your help.
  53.  
  54. Bye!
  55.  
  56. -- 
  57.  
  58. My name is BEN.
  59.